package com.nativetalk.controller.app.two.index;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.banner.TdNtBannerInfoIndex;
import com.nativetalk.bean.course.TsNtCourseInfoIndexTwo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.controller.app.two.setmeal.TwoSetmealController;
import com.nativetalk.service.live.search.LiveSearchService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class TwoIndexController {
    private static Logger logger = Logger.getLogger(TwoIndexController.class);
    @Resource
    private LiveSearchService liveSearchService;
    /**
     * @Purpose  获取banner图信息,直播预告,主类别,课程
     * @version  2.1
     * @time     2015-05-09
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value="/app/5/index" ,method = {RequestMethod.GET,RequestMethod.POST} ,consumes = "application/json" )
    @ResponseBody
    public RetInfo list( @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo = new RetInfo();
            Map<String , Object> map = new HashMap<>();
            List<TdNtBannerInfoIndex> tdNtBannerInfoIndices = (List<TdNtBannerInfoIndex>) MemcachedUtils.get(MemcachedKey.bannerInfoAPPIndexs);
            map.put("banner",tdNtBannerInfoIndices);
            Map<String , List<TsNtCourseInfoIndexTwo>> courseMapTwo =
                    ( Map<String , List<TsNtCourseInfoIndexTwo>>)MemcachedUtils.get(MemcachedKey.tsNtCourseInfoIndexMapTwo);
            map.put("course" , courseMapTwo);
            if (version < 8 ) {
                map.put("livies" , liveSearchService.findTdNtLiveCourseHisByMembIdListIndex());
            }
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取首页数据成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

}


